1 //Robado de: http://www.wretch.cc/blog/celiaailec/19559822
2 // Q562: Dividing coins
4 // Method: Dynamic Programming (0-1 knapsack)
14 return (n
> 0)? n
: -n
;
16 int imax(int n
, int m
)
18 return (n
> m
)? n
: m
;
25 int i
, j
, t
, n
, m
, sum
;
32 for(sum
= 0, i
= 1; i
<= n
; i
++)
33 scanf("%d", &a
[i
]), sum
+= a
[i
];
37 for(i
= 1; i
<= n
; i
++)
39 for(j
= 1; j
<= m
; j
++)
41 v
[i
][j
] = imax(v
[i
-1][j
], v
[i
][j
-1]);
44 v
[i
][j
] = imax(v
[i
][j
], a
[i
] + v
[i
-1][j
-a
[i
]]);
48 printf("%d\n", iabs(sum
- v
[n
][m
] * 2));